<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="出租屋编号" prop="rentalCode">
        <el-input
          v-model="queryParams.rentalCode"
          placeholder="请输入出租屋编号"
          style="width: 240px"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出租屋类型" prop="rentalType">
        <el-select v-model="queryParams.rentalType" style="width: 240px" placeholder="请选择出租屋类型" clearable>
          <el-option
            v-for="dict in rental_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出租状态" prop="rentalStatus">
        <el-select v-model="queryParams.rentalStatus" placeholder="请选择出租状态" clearable>
          <el-option
            v-for="dict in rental_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['rental:rental:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['rental:rental:edit']"
        >修改</el-button>
      </el-col>-->
<!--      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['rental:rental:remove']"
        >删除</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['rental:rental:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rentalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="rentalId" />-->
      <el-table-column label="序号" align="center" width="50" type="index" />
      <el-table-column label="出租屋编号" align="center" width="250" prop="rentalCode" >
        <template #default="scope">
          <el-link type="primary" @click="getRentalDetail(scope.row)" v-hasPermi="['rental:rental:list']">
            {{ scope.row.rentalCode }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="出租屋图片" align="center" prop="rentalImages" >
        <template #default="scope">
          <image-preview :src="scope.row.rentalImages" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="出租屋标题" align="center" prop="rentalTitle" />
      <el-table-column label="出租屋类型" align="center" prop="rentalType">
        <template #default="scope">
          <dict-tag :options="rental_type" :value="scope.row.rentalType"/>
        </template>
      </el-table-column>
      <el-table-column label="出租价格" align="center" prop="rentalPrice" />
      <el-table-column label="出租面积" align="center" prop="rentalArea" />
      <el-table-column label="出租状态" align="center" prop="rentalStatus">
        <template #default="scope">
          <dict-tag :options="rental_status" :value="scope.row.rentalStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center"
                       width="200px"
                       fixed="right" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit"
                     @click="handleUpdate(scope.row)"
                     v-if="showButton(scope.row)"
                     v-hasPermi="['rental:rental:edit']">修改
          </el-button>
          <el-button link type="primary" icon="Delete"
                     @click="handleDelete(scope.row)"
                     v-if="showButton(scope.row)"
                     v-hasPermi="['rental:rental:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改出租屋信息对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="rentalRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="出租屋标题" prop="rentalTitle">
          <el-input v-model="form.rentalTitle" placeholder="请输入出租屋标题" />
        </el-form-item>
        <el-form-item label="出租屋描述" prop="rentalDescription">
          <el-input v-model="form.rentalDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="出租屋地址" prop="rentalLocation">
          <el-input v-model="form.rentalLocation" placeholder="请输入出租屋地址" />
        </el-form-item>
        <el-form-item label="出租屋类型" prop="rentalType">
          <el-select v-model="form.rentalType" placeholder="请选择出租屋类型">
            <el-option
              v-for="dict in rental_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出租价格" prop="rentalPrice">
          <el-input v-model="form.rentalPrice" placeholder="请输入出租价格" />
        </el-form-item>
        <el-form-item label="出租面积" prop="rentalArea">
          <el-input v-model="form.rentalArea" placeholder="请输入出租面积" />
        </el-form-item>
        <el-form-item label="配套设施" prop="rentalFacilities">
          <el-select v-model="form.rentalFacilities" multiple :auto-width="true" placeholder="请选择产品">
            <el-option
                v-for="dict in rental_facilities"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出租屋图片" prop="rentalImages">
          <image-upload v-model="form.rentalImages"/>
        </el-form-item>
        <el-form-item label="出租状态" prop="rentalStatus">
          <el-select v-model="form.rentalStatus" placeholder="请选择出租状态">
            <el-option
              v-for="dict in rental_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系人姓名" prop="contactName">
          <el-input v-model="form.contactName" placeholder="请输入联系人姓名" />
        </el-form-item>
        <el-form-item label="联系人电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系人电话" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 详情 -->
    <el-dialog title="出租屋信息详情" v-model="rentalDetailOpen" width="800px" append-to-body>
      <el-descriptions border :column="1">
        <el-descriptions-item label="出租屋编号">{{form.rentalCode}}</el-descriptions-item>
        <el-descriptions-item label="出租屋标题">{{ form.rentalTitle }}</el-descriptions-item>
        <el-descriptions-item label="出租屋描述">{{ form.rentalDescription }}</el-descriptions-item>
        <el-descriptions-item label="出租屋图片"><image-preview :src="form.rentalImages" :width="50" :height="50"/></el-descriptions-item>
        <el-descriptions-item label="出租屋地址">{{ form.rentalLocation }}</el-descriptions-item>
        <el-descriptions-item label="出租屋类型">
          <dict-tag :options="rental_type" :value="form.rentalType" />
        </el-descriptions-item>
        <el-descriptions-item label="出租价格">
          {{ form.rentalPrice }}
        </el-descriptions-item>
        <el-descriptions-item label="出租面积">{{ form.rentalArea }}</el-descriptions-item>
        <el-descriptions-item label="配套设施">
          <dict-tag v-for="(facilityValue, index) in form.rentalFacilities.split(',')" :key="index" :options="rental_facilities" :value="facilityValue" />
        </el-descriptions-item>
        <el-descriptions-item label="出租状态">
          <dict-tag :options="rental_status" :value="form.rentalStatus" />
        </el-descriptions-item>
        <el-descriptions-item label="联系人姓名">{{ form.contactName }}</el-descriptions-item>
        <el-descriptions-item label="联系人电话">{{ form.contactPhone }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ form.createTime }}</el-descriptions-item>
        <el-descriptions-item label="创建人">{{ form.createBy }}</el-descriptions-item>
        <el-descriptions-item label="修改时间">{{ form.updateTime }}</el-descriptions-item>
        <el-descriptions-item label="修改人">{{ form.updateBy }}</el-descriptions-item>
        <el-descriptions-item label="备注" v-if="form.remark && form.remark.trim() !== ''" width="100">
          <el-input v-model="form.remark" type="textarea" :readOnly="true" disabled />
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup name="Rental">
import { listRental, getRental, delRental, addRental, updateRental } from "@/api/rental/rental";
import download from "@/plugins/download.js";
import useUserStore from "@/store/modules/user.js";
import auth from "@/plugins/auth.js";

const { proxy } = getCurrentInstance();
const { rental_status, rental_type, rental_facilities } = proxy.useDict('rental_status', 'rental_type', 'rental_facilities');

const rentalList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    rentalCode: null,
    rentalType: null,
    rentalStatus: null,
  },
  rules: {
    rentalTitle: [
      { required: true, message: "出租屋标题不能为空", trigger: "blur" }
    ],
    rentalDescription: [
      { required: true, message: "出租屋描述不能为空", trigger: "blur" }
    ],
    rentalLocation: [
      { required: true, message: "出租屋地址不能为空", trigger: "blur" }
    ],
    rentalType: [
      { required: true, message: "出租屋类型不能为空", trigger: "change" }
    ],
    rentalPrice: [
      { required: true, message: "出租屋价格不能为空", trigger: "blur" }
    ],
    rentalArea: [
      { required: true, message: "出租屋面积不能为空", trigger: "blur" }
    ],
    rentalStatus: [
      { required: true, message: "出租屋状态不能为空", trigger: "change" }
    ],
    contactName: [
      { required: true, message: "联系人姓名不能为空", trigger: "blur" }
    ],
    contactPhone: [
      { required: true, message: "联系人电话不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

// 自定义区域
// 当前用户名
const name = useUserStore().name;
const rentalDetailOpen = ref(false);

// 是否展示修改和删除按钮
function showButton(row) {
  // 获取用户角色
  if (auth.hasRole("admin")){
    // 是管理员
    return true
  }
  if(auth.hasRole("rental")) {
    // 是rental角色，此时是信息创建人时，展示修改和删除按钮
    return row.createBy === name
  }
  return false
}

/** 查询出租屋信息列表 */
function getList() {
  loading.value = true;
  listRental(queryParams.value).then(response => {
    rentalList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    rentalId: null,
    rentalCode: null,
    rentalTitle: null,
    rentalDescription: null,
    rentalLocation: null,
    rentalType: null,
    rentalPrice: null,
    rentalArea: null,
    rentalFacilities: [],
    rentalImages: null,
    rentalStatus: null,
    contactName: null,
    contactPhone: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null,
    delFlag: null
  };
  proxy.resetForm("rentalRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.rentalId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加出租屋信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _rentalId = row.rentalId || ids.value
  getRental(_rentalId).then(response => {
    form.value = response.data;
    form.value.rentalFacilities = form.value.rentalFacilities.split(",");
    open.value = true;
    title.value = "修改出租屋信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["rentalRef"].validate(valid => {
    if (valid) {
      form.value.rentalFacilities = form.value.rentalFacilities.join(",");
      if (form.value.rentalId != null) {
        updateRental(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRental(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _rentalIds = row.rentalId || ids.value;
  proxy.$modal.confirm('是否确认删除出租屋信息编号为"' + _rentalIds + '"的数据项？').then(function() {
    return delRental(_rentalIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('rental/rental/export', {
    ...queryParams.value
  }, `rental_${new Date().getTime()}.xlsx`)
}

getList();

// 自定义区域
// 获取流动人口详情
function getRentalDetail(row) {
  reset();
  const _id = row.rentalId;
  getRental(_id).then(response => {
    form.value = response.data;
    rentalDetailOpen.value = true;
  })
}
</script>
